package com.mhc.haval.controller;

import com.mhc.haval.bo.DepartmentBO;
import com.mhc.haval.constant.MsgCodeEnum;
import com.mhc.haval.exception.HavalCommonException;
import com.mhc.haval.form.DeptForm;
import com.mhc.haval.form.ProvinceForm;
import com.mhc.haval.service.DepartmentService;
import com.mhc.haval.service.StaffService;
import com.mhc.haval.vo.DepartmentInfoVO;
import com.mhc.suzuki.api.model.ProvinceDTO;
import com.mhc.suzuki.api.service.IProvinceService;
import com.mhc.suzuki.bo.ProvinceBO;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.subaru.common.entity.BizResult;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by HMJ on 2017/8/15.
 */
@Slf4j
@RestController
@RequestMapping(value = "/dept")     // 通过这里配置使下面的映射都在/dept下，可去除
public class DeptController {

    @Autowired
    private DepartmentService departmentService;
	
	@Autowired
    private StaffService staffService;

    @Autowired
    private IProvinceService iProvinceService;

    /**
     * 新建部门
     *
     * @param deptForm 部门表单信息
     * @return
     */
    @ApiOperation(value = "新建部门", httpMethod = "POST", notes = "创建部门")
    @ApiImplicitParam(name = "deptForm", value = "部门详细实体", required = true, paramType = "body", dataType = "DeptForm")
    @RequestMapping(value = "/saveDept", method = RequestMethod.POST)
    public BizResult saveDept(@RequestBody DeptForm deptForm) {
        try {
            DepartmentBO departmentBO = new DepartmentBO();
            BeanUtils.copyProperties(deptForm, departmentBO);
            boolean insertFlag = departmentService.addDeptInfo(departmentBO);
            if (insertFlag) {
                log.info("新建部门完成...");
                return BizResult.create(insertFlag, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
            } else {
                log.info("存在相同部门名称...");
                return BizResult.create(MsgCodeEnum.DEPT_EXISTS.getErrCode(), MsgCodeEnum.DEPT_EXISTS
                        .getErrMsg());
            }
        } catch (HavalCommonException e) {
            log.error("saveDept HavalCommonException", e);
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }

    /**
     * 修改部门信息
     *
     * @param deptForm 部门表单信息
     * @return
     */
    @ApiOperation(value = "修改部门信息", httpMethod = "PUT", notes = "更新部门信息")
    @ApiImplicitParam(name = "deptForm", value = "部门详细实体", required = true, paramType = "body", dataType = "DeptForm")
    @RequestMapping(value = "/editDept", method = RequestMethod.PUT)
    public BizResult editDept(@RequestBody DeptForm deptForm) {
        try {
            DepartmentBO departmentBO = new DepartmentBO();
            BeanUtils.copyProperties(deptForm, departmentBO);
            boolean updateFlag = departmentService.editDeptInfo(departmentBO);
            log.info("修改部门成功...");
            return BizResult.create(updateFlag, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (Exception e) {
            log.error("修改部门异常: " + e.getMessage(), e);
            return BizResult.create(MsgCodeEnum.EXCEPTION.getErrCode(), MsgCodeEnum.EXCEPTION.getErrMsg());
        }
    }


    /**
     * 获取部门列表
     *
     * @param parentDeptId 上级部门ID
     * @param companyNo    公司ID
     * @return
     */
    @ApiOperation(value = "获取部门列表", httpMethod = "GET", notes = "根据查询条件获取部门列表")
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentDeptId", value = "上级部门ID", required = false, paramType = "query",
                    dataType = "Long"),
            @ApiImplicitParam(name = "companyNo", value = "公司ID", required = true, paramType = "query", dataType =
                    "Long")
    })
    public BizResult list(@RequestParam(value = "parentDeptId", required = false, defaultValue = "") Long parentDeptId,
                          @RequestParam(value = "companyNo", required = true, defaultValue = "") String companyNo) {
        try {
            List<DepartmentInfoVO> list = departmentService.selectByDeptList(parentDeptId, companyNo);
            log.info("获取部门列表成功,List:" + list);
            return BizResult.create(list, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (HavalCommonException e) {
            log.error("deptList HavalCommonException", e);
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }

    /**
     * 获取部门详细信息
     *
     * @param deptId 部门ID
     * @return
     */
    @ApiOperation(value = "获取部门详细信息", notes = "根据url的id来获取部门详细信息")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, paramType = "path", dataType = "Long")
    @RequestMapping(value = "/look/{deptId}", method = RequestMethod.GET)
    public BizResult getDeptInfo(@PathVariable Long deptId) {
        try {
            DepartmentInfoVO departmentInfoVO = departmentService.queryByInfo(deptId);
            log.info("获取部门详细信息成功");
            return BizResult.create(departmentInfoVO, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg
                    ());
        } catch (HavalCommonException e) {
            log.error("getDeptInfo HavalCommonException", e);
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }


    /**
     * 删除部门
     *
     * @param deptId 部门ID
     * @return
     */
    @ApiOperation(value = "删除部门", notes = "根据id来删除部门")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, paramType = "path", dataType = "Long")
    @RequestMapping(value = "/delete/{deptId}", method = RequestMethod.GET)
    public BizResult deleteDept(@PathVariable Long deptId) {
        try {
            // 逻辑删除部门
            return departmentService.delDept(deptId);
        } catch (Exception e) {
            log.error("删除部门异常: " + e.getMessage(), e);
            return BizResult.create(MsgCodeEnum.EXCEPTION.getErrCode(), MsgCodeEnum.EXCEPTION.getErrMsg());
        }
    }


    /**
     * 查询省份列表
     *
     * @param provinceForm
     * @return
     */
    @ApiOperation(value = "查询省份列表", httpMethod = "PUT", notes = "查询省份列表")
    @ApiImplicitParam(name = "provinceForm", value = "省份详细实体", required = true, paramType = "body", dataType =
            "ProvinceForm")
    @RequestMapping(value = "/getProvinceList", method = RequestMethod.GET)
    public BizResult getProvinceList(ProvinceForm provinceForm) {
        try {
            ProvinceBO provinceBO = new ProvinceBO();
            BeanCopierUtil.copy(provinceForm, provinceBO);
            List<ProvinceDTO> provinceList = iProvinceService.getProvinceList(provinceBO);
            log.info("查询省份列表成功...");
            return BizResult.create(provinceList, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (SuzukiCommonException e) {
            log.error("getProvinceList SuzukiCommonException", e);
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }

}
